package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.Attraction;

public class ListAttractionAdapter extends ArrayAdapter<Attraction> {
	private Activity context = null;
	private int resource;
	private ArrayList<Attraction> arr = null;

	// variable for imageloader
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	public ListAttractionAdapter(Activity context, int resource,
			ArrayList<Attraction> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.arr = objects;

		// setup image loader
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getContext()).build();
		if (imageLoader != null) {
			imageLoader.destroy();
		}
		if (options != null) {
			options = null;
		}
		if (ImageLoader.getInstance().isInited()) {
			ImageLoader.getInstance().destroy();
		}
		ImageLoader.getInstance().init(config);
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_photos)
				.showImageForEmptyUri(R.drawable.no_image)
				.showImageOnFail(R.drawable.no_image).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		try {

			// set view for list view item
			LayoutInflater inflater = context.getLayoutInflater();
			convertView = inflater.inflate(resource, null);
			Attraction attraction = arr.get(position);

			ImageView attImage = (ImageView) convertView
					.findViewById(R.id.attImage);
			TextView attTitle = (TextView) convertView
					.findViewById(R.id.attTitle);
			TextView attCategory = (TextView) convertView
					.findViewById(R.id.attCategory);
			ImageView attQuality = (ImageView) convertView
					.findViewById(R.id.attQuality);

			// set place icon
			imageLoader.displayImage(arr.get(position).getIconImage(),
					attImage, options, null);
			attImage.setScaleType(ScaleType.FIT_CENTER);

			// set quality icon
			switch (attraction.getQuality()) {
			case 5:
				attQuality.setImageResource(R.drawable.ic_five_star);
				break;
			case 4:
				attQuality.setImageResource(R.drawable.ic_four_star);
				break;
			case 3:
				attQuality.setImageResource(R.drawable.ic_three_star);
				break;
			case 2:
				attQuality.setImageResource(R.drawable.ic_two_star);
				break;
			case 1:
				attQuality.setImageResource(R.drawable.ic_one_star);
				break;
			default:
				attQuality.setImageResource(R.drawable.ic_zero_star);
				break;
			}

			// set category
			attCategory.setText(attraction.getType());
			attTitle.setText(attraction.getName());

			return convertView;
		} catch (Exception e) {
			Log.e("ListAttractionAdapter", e.toString());
			return null;
		} finally {
			// imageLoader.destroy();
			// options.
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	// @Override
	// public void notifyDataSetChanged() {
	// // TODO Auto-generated method stub
	// clear();
	// super.notifyDataSetChanged();
	// }

	public void setArr(ArrayList<Attraction> arr) {
		this.arr = arr;
	}

}
